#include "fromchknet.h"
#include "ui_fromchknet.h"
#include <QMessageBox>
#include <QComboBox>
#include <QSplashScreen>

fromchknet::fromchknet(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::fromchknet)
{
    ui->setupUi(this);
    QPixmap pixmap(":/tree/start.jpg"); // 装载图片
    QSplashScreen splash(pixmap); // 初始化图片到QSplashScreen
    splash.show(); // 显示图片
    //this->show();
    //this->repaint();
    //:/ICO/chkNet1.ico
    this->setWindowIcon(QIcon(":/ICO/chkNet1.ico"));
    Start();
    RefPIcture();
    splash.finish(this); // 完成后自动close
}

fromchknet::~fromchknet()
{
    delete ui;
}


QString fromchknet::protocolName(QAbstractSocket::NetworkLayerProtocol protocol)
{
    switch (protocol) {
    case QAbstractSocket::IPv4Protocol:
        return "IPv4 Protocol";
    case QAbstractSocket::IPv6Protocol:
        return "IPv6 Protocol";
    case QAbstractSocket::AnyIPProtocol:
        return "Any IP Protocol";
    default:
        return "Unknown Network Layer Protocol";
    }

}
QString fromchknet::ReplaceIimg(QString str){
    QString ImgStr="";
    ImgStr="<img src=\":/tree/all.png\"  align=\"left\" height=48 width=48>";
    if (str.contains("打印机",Qt::CaseSensitive)) ImgStr="<img src=\":/tree/print.png\"  align=\"left\" height=48 width=48>";
    if (str.contains("服务器",Qt::CaseSensitive)) ImgStr="<img src=\":/tree/server.png\"  align=\"left\" height=48 width=48>";
    if (str.contains("主机",Qt::CaseSensitive)) ImgStr="<img src=\":/tree/server.png\"  align=\"left\" height=48 width=48>";
    if (str.contains("NAS",Qt::CaseSensitive)) ImgStr="<img src=\":/tree/nas.png\"  align=\"left\" height=48 width=48>";
    if (str.contains("摄像头",Qt::CaseSensitive)) ImgStr="<img src=\":/tree/cam.png\"  align=\"left\" height=48 width=48>";
    if (str.contains("监控",Qt::CaseSensitive)) ImgStr="<img src=\":/tree/cam.png\"  align=\"left\" height=48 width=48>";
    if (str.contains("路由器",Qt::CaseSensitive)) ImgStr="<img src=\":/tree/net.png\"  align=\"left\" height=48 width=48>";
    if (str.contains("计算机",Qt::CaseSensitive)) ImgStr="<img src=\":/tree/pc.png\"  align=\"left\" height=48 width=48>";
    if (str.contains("无线",Qt::CaseSensitive)) ImgStr="<img src=\":/tree/wifi.png\"  align=\"left\" height=48 width=48>";
    if (str.contains("网站",Qt::CaseSensitive)) ImgStr="<img src=\":/tree/web_server.png\"  align=\"left\" height=48 width=48>";
    if (str.contains("网页",Qt::CaseSensitive)) ImgStr="<img src=\":/tree/web_server.png\"  align=\"left\" height=48 width=48>";
    if (str.contains("数据库",Qt::CaseSensitive)) ImgStr="<img src=\":/tree/database.png\"  align=\"left\" height=48 width=48>";
    return ImgStr;

}

void fromchknet::Start(){//初始化
    this->setWindowTitle("网络检测工具 1.0 ");
    ui->labelWeb->setText(tr("<a href=\"https://jinesc.cn\">风帆小筑 www.jinesc.cn</a> jinesc@163.com QQ:475847"));
    connect(ui->labelWeb, &QLabel::linkActivated, this, [=](const QString& link){
            QDesktopServices::openUrl(QUrl(link));
        });
    //QMessageBox::information(NULL, "信息", QSysInfo::kernelType());
    //QMessageBox::information(NULL, "信息", QSysInfo::kernelVersion());

    QString curPath= QCoreApplication::applicationDirPath();//获取当前目录
    //建立LOG目录
    QDir *folder = new QDir;
    QString LogPath =QString("%1/log").arg(curPath);
    //folder->mkdir(QString("%1/tmp").arg(aFile));
    folder->mkdir(LogPath);

    QString gifname = QString("%1/logo.gif").arg(curPath);

    if (JCode::isFileExist(gifname)==false) //文件不存在就下载文件
    {
        QString url="http://jinesc.cn/zb_users/upload/2020/12/202012101607582307549973.gif";
        setNetworkGIF(url);
    }else{
        //存在就直接打开文件
        QMovie *movie = new QMovie( gifname); //path是gif图片的绝对路径
        movie->setScaledSize(ui->labGif->size());
        ui->labGif->setMovie(movie);
        movie->start();
    }


    ui->treeWidgetIpScan->clear();
    ui->treeWidgetIpScan->setHeaderLabels(QStringList() << "IP" << "计算机名" << "MAC" );
    ui->treeWidgetIpScan->header()->setSectionResizeMode(QHeaderView::ResizeToContents); //QTreeWidget设置根据内容自适应宽度
    QString str="";

    QString InfoHtml="";

    if (QSysInfo::kernelType().contains("win",Qt::CaseSensitive)){
        str=JCode::SYSINFO();
        InfoHtml+="<h1>系统信息</h1>";
        InfoHtml+=QString("<table border='1' cellspacing='0' width='100%' >%1</table>").arg(str);
    }
    if (QSysInfo::kernelType().contains("linux",Qt::CaseSensitive)){
        str=JCode::CMD("uname -a");
        InfoHtml+="<h1>系统信息</h1>";
        InfoHtml+=QString("%1").arg(str);
        str=JCode::GetIPadd();
        InfoHtml+="<h1>网络</h1>";
        InfoHtml+=QString("<table border='1' cellspacing='0' width='100%' >%1</table>").arg(str);
    }

    ui->txtINFO->setHtml(InfoHtml);

    QString localHostName = QHostInfo::localHostName();
    ui->labelInfo->setText(localHostName);
    //tmp+= QString("主机名:%1").arg(localHostName);
    //str=str.replace("\n"," ");
    //读取脚本内容
    curPath= QCoreApplication::applicationDirPath();//获取系统当前目录
    QString iniFile=QString("%1/chkScript.txt").arg(curPath);
    str=JCode::OpenText(iniFile);
    //str=str.toUpper(); //切换大写
    QStringList list1;
    list1.clear();
    list1<<"别名"<<"类型"<<"CMD/URL"<<"成功字符"<<"位置";
    ui->tableScript->clear();
    ui->tableScript->setColumnCount(list1.size());
    ui->tableScript->setHorizontalHeaderLabels(list1);
    //设置列宽
    ui->tableScript->setColumnWidth(0, 200);
    ui->tableScript->setColumnWidth(1, 80);
    ui->tableScript->setColumnWidth(2, 300);
    ui->tableScript->setColumnWidth(3, 200);
    ui->tableScript->setColumnWidth(4, 120);

    ui->tableScript->verticalHeader()->setFixedWidth(48);
    ui->tableScript->verticalHeader()->setDefaultSectionSize(48); //设置行距
    ui->tableScript->horizontalHeader()->setFixedHeight(48); //设置表头的高度
    ui->tableScript->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;}");
    ui->tableScript->setStyleSheet("selection-background-color:lightblue;"); //设置选中背景色
    ui->tableScript->setRowCount(2); //暂时设置成2行
    //ui->tableScript->setEditTriggers(QAbstractItemView::NoEditTriggers); //可编辑
    ui->tableScript->setSelectionMode(QAbstractItemView::ExtendedSelection);//单选
    ui->tableScript->repaint();
    int Tline=-1;
    QStringList strList = str.split("\n");
    for(int i = 0; i < strList.size()-1; i++) {
        QString str1 = strList[i].replace(QRegExp("\\s{1,}"), " ");
        QStringList subList = str1.split("|");
        if (subList.size()>1){
            Tline++;
            if (Tline>=ui->tableScript->rowCount()-1){
                int row = ui->tableScript->rowCount();
                    ui->tableScript->insertRow(row);
            }
            //str1=subList[0].toUpper();
            //list1<<"别名"<<"类型"<<"CMD/URL"<<"成功字符";
           if (subList.size()>0)  ui->tableScript->setItem(Tline,0,new QTableWidgetItem(subList[2].trimmed()));
           if (subList.size()>1)  ui->tableScript->setItem(Tline,1,new QTableWidgetItem(subList[0].trimmed()));
           if (subList.size()>2)  ui->tableScript->setItem(Tline,2,new QTableWidgetItem(subList[1].trimmed()));
           if (subList.size()>3) ui->tableScript->setItem(Tline,3,new QTableWidgetItem(subList[3].trimmed()));
           if (subList.size()>4) ui->tableScript->setItem(Tline,4,new QTableWidgetItem(subList[4].trimmed()));
        }
    }


    list1.clear();
    list1<<"名称"<<"状态"<<"检测内容"<<"检测时间"<<"检测结果";
    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(list1.size());
    ui->tableWidget->setHorizontalHeaderLabels(list1);
    //设置列宽
    ui->tableWidget->setColumnWidth(0, 240);
    ui->tableWidget->setColumnWidth(1, 80);
    ui->tableWidget->setColumnWidth(2, 400);
    ui->tableWidget->setColumnWidth(3, 240);
    ui->tableWidget->setColumnWidth(4, 660);

    ui->tableWidget->verticalHeader()->setFixedWidth(48);
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(48); //设置行距
    ui->tableWidget->horizontalHeader()->setFixedHeight(48); //设置表头的高度
    ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;}");
    ui->tableWidget->setStyleSheet("selection-background-color:lightblue;"); //设置选中背景色



    ui->tableWidget->setRowCount(2); //暂时设置成2行
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //不可编辑
    ui->tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);//单选
    ui->tableWidget->repaint();

    //处理tabWidget

    ui->tabWidget->setTabText(1,"批量检测");
    ui->tabWidget->setTabText(2,"设置批量检测");
    ui->tabWidget->setTabText(3,"IP搜索");
    ui->tabWidget->setTabText(4,"CMD命令行");
    ui->tabWidget->setTabText(5,"网站查阅");
    ui->tabWidget->setTabText(6,"关于");

    ui->tabWidget->setCurrentIndex(0);
    /*ui->tabWidget->setStyleSheet("QTabBar::tab { background: gray; color: white; padding: 10px; } "
                                 "QTabBar::tab:selected { background: black; } " );
                                 */

    QString tabBarStyle = "QTabBar::tab {min-width:100px;color: black;background-color:qlineargradient(x1:0, y1:0, x2:0, y2:1, stop: 0 #eeeeee, stop: 1 gray);border: 2px solid;border-top-left-radius: 10px;border-top-right-radius: 10px;padding:5px;}\
    QTabBar::tab:!selected {margin-top: 10px;} \
    QTabBar::tab:selected {color: blue;}";
    ui->tabWidget->setStyleSheet(tabBarStyle);

    QString Tstr=JCode::getUrlString("https://jinesc.cn/JSoft/chkNet.htm");

    ui->txtAbout->setHtml(Tstr);
    Tstr=JCode::getUrlString("https://jinesc.cn/JSoft/chkNetAD.html");
    ui->labelAD->setText(Tstr);

    ui->cmbKeep->addItem("手动执行");
    ui->cmbKeep->addItem("1分钟自动执行");
    ui->cmbKeep->addItem("10分钟自动执行");
    ui->cmbKeep->addItem("30分钟自动执行");
    ui->cmbKeep->addItem("1小时自动执行");
    ui->cmbKeep->addItem("2小时自动执行");
    ui->cmbKeep->addItem("3小时自动执行");
    ui->cmbKeep->addItem("5小时自动执行");
    ui->cmbKeep->addItem("6小时自动执行");
    ui->cmbKeep->addItem("8小时自动执行");
    ui->cmbKeep->addItem("12小时自动执行");
    ui->cmbKeep->addItem("24小时自动执行");
    ui->cmbKeep->setCurrentIndex(0);
    ui->chkSpack->setVisible(false); //linux下关闭语音提醒
    if (QSysInfo::kernelType().contains("win",Qt::CaseSensitive)){
       ui->chkSpack->setVisible(true);
    }

    m_timer = new QTimer(this);
    connect(m_timer, SIGNAL(timeout()), this, SLOT(AutoKeep()));
    djs = new QTimer(this);
    connect(djs, SIGNAL(timeout()), this, SLOT(DJS()));
    djs->start(1000);
    js=-1; //负数不循环





    ui->tableWidget->setVisible(true);
    ui->widgetPic->setVisible(false);

}

void fromchknet::on_cmdScanIP_clicked()
{
    QHostAddress test;
    if (!test.setAddress(ui->txtSIPadd->text()))
    {
        QMessageBox::information(this,"提示信息","起始IP地址错误！");
        return;
    }
    if (!test.setAddress(ui->txtEIPadd->text()))
    {
        QMessageBox::information(this,"提示信息","结束IP地址错误！");
        return;
    }
    int sip=0;
    int eip=0;
    QString str=ui->txtSIPadd->text().trimmed();
    sip=str.mid(str.lastIndexOf ( "." )+1).toInt();
    str=str.left(str.lastIndexOf ( "." ));
    QString str1=ui->txtEIPadd->text().trimmed();
    eip=str1.mid(str1.lastIndexOf ( "." )+1).toInt();
    str1=str1.left(str1.lastIndexOf ( "." ));
    //QMessageBox::information(this,"提示信息",QString("%1\n%2").arg(str).arg(str1));
    //QMessageBox::information(this,"提示信息",QString("%1\n%2").arg(sip).arg(eip));
    if (str1!=str)
    {
        QMessageBox::information(this,"提示信息","起始和结束IP地址不在一个网段里！");
        return;
    }

    str="";
    for(int i=sip;i<=eip;i++){
           QString ipadd=QString("%1.%2").arg(str1).arg(i);
           QString tmpstr="";
           tmpstr=JCode::PingIP(ipadd);

           str=QString("Ping %1 >> %2\n%3").arg(ipadd).arg(tmpstr).arg(str);
           if (tmpstr=="成功"){

                bool f=false;
                QTreeWidgetItemIterator it(ui->treeWidgetIpScan);
                while (*it) {
                    //(*it)->text(0);
                   if ((*it)->text(0)==str1){
                       QStringList child;
                       child.clear();
                       child << ipadd;
                       child << "";
                       child << "";
                       QTreeWidgetItem * citem =new QTreeWidgetItem(child);
                       (*it)->addChild(citem);
                       QIcon icon1;
                       icon1.addPixmap(QPixmap(":/tree/MYCOMP.ICO"), QIcon::Selected);
                       icon1.addPixmap(QPixmap(":/tree/MYCOMP.ICO"), QIcon::Normal);
                       citem->setIcon(0, icon1);
                       f=true;
                   }
                   ++it;
                }
                if (f==false){
                    { //加顶级
                        QStringList child;
                        child.clear();
                        child << str1;
                        child << "";
                        child << "";
                        QTreeWidgetItem * citem =new QTreeWidgetItem(child);
                        ui->treeWidgetIpScan->addTopLevelItem(citem);
                        QIcon icon;
                        icon.addPixmap(QPixmap(":/tree/DRIVENET.ICO"), QIcon::Selected);
                        icon.addPixmap(QPixmap(":/tree/DRIVENET.ICO"), QIcon::Normal);
                        citem->setIcon(0, icon);


                        //加孩子
                        QStringList child1;
                        child1.clear();
                        child1 << ipadd;
                        child1 << "";
                        child1 << "";
                        QTreeWidgetItem * citem1 =new QTreeWidgetItem(child1);
                        citem->addChild(citem1);
                        QIcon icon1;
                        icon1.addPixmap(QPixmap(":/tree/MYCOMP.ICO"), QIcon::Selected);
                        icon1.addPixmap(QPixmap(":/tree/MYCOMP.ICO"), QIcon::Normal);
                        citem1->setIcon(0, icon1);
                    }
                }
           }

           ui->txtJG->setText(str);
           ui->txtJG->repaint();  //因为setText()不会立即调用paintEvent进行绘图。程序会等你退出循环后才有机会进行绘图。
           ui->txtJG->update();

           //解决的办法是：        label->setText()后，调用label->repaint()立即重绘。
       }
       ui->txtJG->setText(str);


}


void fromchknet::ScriptStart()
{

    RefPIcture();
    QString curPath= QCoreApplication::applicationDirPath();//获取当前目录
    QDateTime current_date_time = QDateTime::currentDateTime();
    QString LogFile =QString("%1/log/%2.txt").arg(curPath).arg(current_date_time.toString("yyyy年MM月dd日"));

    QStringList list1;
    list1.clear();
    list1<<"名称"<<"状态"<<"检测内容"<<"检测时间"<<"检测结果"<<"位置";
    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(list1.size());
    ui->tableWidget->setHorizontalHeaderLabels(list1);
    //设置列宽
    ui->tableWidget->setColumnWidth(0, 200);
    ui->tableWidget->setColumnWidth(1, 80);
    ui->tableWidget->setColumnWidth(2, 360);
    ui->tableWidget->setColumnWidth(3, 200);
    ui->tableWidget->setColumnWidth(4, 500);
    ui->tableWidget->setColumnWidth(5, 100);

    ui->tableWidget->verticalHeader()->setFixedWidth(48);
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(48); //设置行距
    ui->tableWidget->horizontalHeader()->setFixedHeight(48); //设置表头的高度
    ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;}");
    ui->tableWidget->setStyleSheet("selection-background-color:lightblue;"); //设置选中背景色
    ui->tableWidget->setRowCount(2); //暂时设置成2行
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //不可编辑
    ui->tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);//单选
    ui->tableWidget->repaint();

    //ui->tableWidget->horizontalScrollBar()->setEnabled(false);
    int  Tline=-1; //行号

    QString str="";
    for(int i=0;i<ui->tableScript->rowCount();i++){
        if(ui->tableScript->item(i,0)!=NULL && ui->tableScript->item(i,0)->text().trimmed()!="" ){
            //list1<<"别名"<<"类型"<<"CMD/URL"<<"成功字符";
            if (ui->tableScript->item(i,1)!=NULL) str+=ui->tableScript->item(i,1)->text().trimmed();
            str+="|";
            if (ui->tableScript->item(i,2)!=NULL) str+=ui->tableScript->item(i,2)->text().trimmed();
            str+="|";
            if (ui->tableScript->item(i,0)!=NULL) str+=ui->tableScript->item(i,0)->text().trimmed();
            str+="|";
            if (ui->tableScript->item(i,3)!=NULL) str+=ui->tableScript->item(i,3)->text().trimmed();
            str+="|";
            if (ui->tableScript->item(i,4)!=NULL) str+=ui->tableScript->item(i,4)->text().trimmed();
            str+="\n";
        }
    }
    QStringList strList = str.split("\n");
    for(int i = 0; i < strList.size()-1; i++) {
        QString str1 = strList[i].replace(QRegExp("\\s{1,}"), " ");
        QStringList subList = str1.split("|");
        if (subList.size()>1){
            Tline++;
            if (Tline>=ui->tableWidget->rowCount()-1){
                int row = ui->tableWidget->rowCount();
                    ui->tableWidget->insertRow(row);
            }
            str1=subList[0].toUpper();            
            if (str1=="CMD"){

                //list1<<"名称"<<"状态"<<"检测内容"<<"检测结果";
                str=subList[2].trimmed();
                ui->tableWidget->setItem(Tline,0,new QTableWidgetItem(str));
                str="";
                ui->tableWidget->setItem(Tline,1,new QTableWidgetItem(str));
                str=QString("%1-%2").arg(subList[0]).arg(subList[1]);
                ui->tableWidget->setItem(Tline,2,new QTableWidgetItem(str));
                QDateTime current_date_time =QDateTime::currentDateTime();
                str=current_date_time.toString("yyyy年MM月dd日 hh:mm:ss");
                ui->tableWidget->setItem(Tline,3,new QTableWidgetItem(str));
                str=subList[4].trimmed();
                ui->tableWidget->setItem(Tline,5,new QTableWidgetItem(str));


                str=JCode::CMD(subList[1]).trimmed();
                ui->tableWidget->setItem(Tline,4,new QTableWidgetItem(str));                
                //设置红绿灯
                QWidget *pWidget = new QWidget();
                QHBoxLayout *pLayout = new QHBoxLayout();
                pLayout->setMargin(0);
                QLabel *pLabel = new QLabel();
                setLED(pLabel, 1, 16); //把红绿灯变成红色了
                QLabel *pLabel1 = new QLabel();

                pLabel1->setText("异常");

                pLayout->addWidget(pLabel);
                //pLayout->addWidget(pLabel1);
                pLayout->setAlignment(Qt::AlignLeft);
                //qDebug()<<str<<subList[3].trimmed();
                if (str.contains(subList[3].trimmed(),Qt::CaseSensitive)){
                    setLED(pLabel, 2, 16); //把红绿灯变成绿色了
                    pLabel1->setText("正常");
                }
                pWidget->setLayout(pLayout);
                pWidget->resize(ui->tableWidget->columnWidth(1),48);
                pWidget->setStyleSheet("QWidget{ border: 0px }");
                ui->tableWidget->setCellWidget(Tline,1,pWidget);
                ui->tableWidget->item(Tline,1)->setText(QString("%1").arg(pLabel1->text()));
                ui->tableWidget->item(Tline,1)->setTextAlignment(Qt::AlignCenter);

                ui->tableWidget->repaint();

            }
            if (str1=="URL"){
                QString Tstr=JCode::getUrlString(subList[1]);
                str=subList[2].trimmed();
                ui->tableWidget->setItem(Tline,0,new QTableWidgetItem(str));
                str="";
                ui->tableWidget->setItem(Tline,1,new QTableWidgetItem(str));
                str=QString("%1-%2").arg(subList[0]).arg(subList[1]);
                ui->tableWidget->setItem(Tline,2,new QTableWidgetItem(str));
                QDateTime current_date_time =QDateTime::currentDateTime();
                str=current_date_time.toString("yyyy年MM月dd日 hh:mm:ss");
                ui->tableWidget->setItem(Tline,3,new QTableWidgetItem(str));
                str=Tstr;
                ui->tableWidget->setItem(Tline,4,new QTableWidgetItem(str));
                str=subList[4].trimmed();
                ui->tableWidget->setItem(Tline,5,new QTableWidgetItem(str));
                //设置红绿灯
                QWidget *pWidget = new QWidget();
                QHBoxLayout *pLayout = new QHBoxLayout();
                pLayout->setMargin(0);
                QLabel *pLabel = new QLabel();
                setLED(pLabel, 0, 16); //把红绿灯变成灰色了
                QLabel *pLabel1 = new QLabel();

                pLabel1->setText("待检测");
                pLayout->addWidget(pLabel);
                //pLayout->addWidget(pLabel1);
                pLayout->setAlignment(Qt::AlignLeft);

                if (subList.size()>2){
                    if (Tstr.contains(subList[3].trimmed(),Qt::CaseSensitive)){
                        setLED(pLabel, 2, 16); //把红绿灯变成绿色了
                        pLabel1->setText("正常");

                    }else{

                        setLED(pLabel, 1, 16); //把红绿灯变成红色了
                        pLabel1->setText("异常");
                    }
                    //tmpstr+=QString(" >>>  %2k  %1 \n").arg(Tstr.replace("\n","")).arg(Tstr.length()/1024);
                }
                pWidget->setLayout(pLayout);
                pWidget->resize(ui->tableWidget->columnWidth(1),48);
                pWidget->setStyleSheet("QWidget{ border: 0px }");
                ui->tableWidget->setCellWidget(Tline,1,pWidget);
                ui->tableWidget->item(Tline,1)->setText(QString("%1").arg(pLabel1->text()));
                ui->tableWidget->item(Tline,1)->setTextAlignment(Qt::AlignCenter);

                ui->tableWidget->repaint();
            }
        }
        if (ui->tableWidget->item(Tline,1)->text()!="正常"){//非正常情况
            if (ui->chkSpack->isChecked()==true){
                QString SpeakText="";
                SpeakText+=ui->tableWidget->item(Tline,0)->text().trimmed();
                SpeakText+="发现异常！";
                JCode::SpeakText(SpeakText);
            }

        }
        QList<QLabel*> GQLabels = ui->widgetPic->findChildren<QLabel*>();
        //遍历widgetPic下所有QLabel
        foreach (QLabel* GQLabel, GQLabels) {
            //qDebug()<<GQLabel->text();
            if (GQLabel->toolTip()==QString("%1").arg(i)){
                 QString tmpP="";
                 if(ui->tableScript->item(i,0)!=NULL ) tmpP+=ReplaceIimg(ui->tableScript->item(i,0)->text().trimmed());
                 if (ui->tableWidget->item(i,1)->text().trimmed()=="正常"){
                     tmpP+="<img src=\":/tree/ok.png\" height=32 width=32/><br>";
                 }else{
                     tmpP+="<img src=\":/tree/no.png\" height=32 width=32/><br>";
                 }
                 tmpP+="<span>";
                 tmpP+=ui->tableScript->item(i,0)->text().trimmed();
                 tmpP+="</span>";
                 GQLabel->setText(tmpP);
                 GQLabel->setToolTip(ui->tableWidget->item(i,4)->text().trimmed());
                 GQLabel->repaint();
                 GQLabel->update();

            }
            //等待一会
            QTime t;
            t.start();
            while(t.elapsed()<10*1)
                QCoreApplication::processEvents();

        }
        //写入日志
        QString LogStr="";
        LogStr+=QString("%1,").arg(ui->tableWidget->item(i,5)->text().trimmed());
        LogStr+=QString("%1,").arg(ui->tableWidget->item(i,0)->text().trimmed());
        LogStr+=QString("%1,").arg(ui->tableWidget->item(i,1)->text().trimmed());
        LogStr+=QString("%1,").arg(ui->tableWidget->item(i,2)->text().trimmed());
        LogStr+=QString("%1,").arg(ui->tableWidget->item(i,3)->text().trimmed());
        //LogStr+=QString("%1,").arg(current_date_time.toString("yyyy-MM-dd hh:mm::ss"));
        //LogStr+=QString("%1,").arg(ui->tableWidget->item(i,4)->text().trimmed());

        LogStr+="\n";
        if (JCode::AppendText(LogFile,LogStr)==false){
            qDebug()<<"错误！";
        }

        //list1<<"名称"<<"状态"<<"检测内容"<<"检测时间"<<"检测结果"<<"位置";
        //ui->tableWidget->clear();


    }



}
void fromchknet::setLED(QLabel* label, int color, int size)
{
    // 该函数将label控件变成一个圆形指示灯，需要指定颜色color以及直径size
    // color 0:grey 1:red 2:green 3:yellow
    // size  单位是像素
    // 将label中的文字清空
    label->setText("");
    // 先设置矩形大小
    // 如果ui界面设置的label大小比最小宽度和高度小，矩形将被设置为最小宽度和最小高度；
    // 如果ui界面设置的label大小比最小宽度和高度大，矩形将被设置为最大宽度和最大高度；
    QString min_width = QString("min-width: %1px;").arg(size);              // 最小宽度：size
    QString min_height = QString("min-height: %1px;").arg(size);            // 最小高度：size
    QString max_width = QString("max-width: %1px;").arg(size);              // 最小宽度：size
    QString max_height = QString("max-height: %1px;").arg(size);            // 最小高度：size
    // 再设置边界形状及边框
    QString border_radius = QString("border-radius: %1px;").arg(size/2);    // 边框是圆角，半径为size/2
    QString border = QString("border:1px solid black;");                    // 边框为1px黑色
    // 最后设置背景颜色
    QString background = "background-color:";
    switch (color) {
    case 0:        // 灰色
        background += "rgb(190,190,190)";
        break;
    case 1:        // 红色
        background += "rgb(255,0,0)";
        break;
    case 2:        // 绿色
        background += "rgb(0,255,0)";
        break;
    case 3:        // 黄色
        background += "rgb(255,255,0)";
        break;
    default:
        break;
    }
    const QString SheetStyle = min_width + min_height + max_width + max_height + border_radius + border + background;
    label->setStyleSheet(SheetStyle);
}

void fromchknet::on_cmdCMD_clicked()
{
    QString str=ui->txtCMD->toPlainText();
    QString outStr="";
    str+="\n";
    QStringList strList = str.split("\n");
    for(int i = 0; i < strList.size()-1; i++) {
        if (strList[i].trimmed()!=""){
            outStr+=strList[i].trimmed()+"\n";
            outStr+="-------------------------------------------------\n";
            outStr+=JCode::CMD(strList[i].trimmed());
            outStr+="\n";
        }
    }
    //QString str=JCode::CMD(ui->txtCMD->text()).trimmed();
    ui->textCMD->setText(outStr);
}

void fromchknet::on_cmdUrl_clicked()
{
    QString Tstr=JCode::getUrlString(ui->txtUrl->text());

    ui->txtUrlTxt->setPlainText(Tstr);
    ui->txtUrlHtml->setHtml(Tstr);
}



void fromchknet::on_cmdSet_SaveScript_clicked()
{
    QString str="";
    for(int i=0;i<ui->tableScript->rowCount();i++){
        if(ui->tableScript->item(i,0)!=NULL && ui->tableScript->item(i,0)->text().trimmed()!="" ){
            //list1<<"别名"<<"类型"<<"CMD/URL"<<"成功字符";
            if (ui->tableScript->item(i,1)!=NULL) str+=ui->tableScript->item(i,1)->text().trimmed();
            str+="|";
            if (ui->tableScript->item(i,2)!=NULL) str+=ui->tableScript->item(i,2)->text().trimmed();
            str+="|";
            if (ui->tableScript->item(i,0)!=NULL) str+=ui->tableScript->item(i,0)->text().trimmed();
            str+="|";
            if (ui->tableScript->item(i,3)!=NULL) str+=ui->tableScript->item(i,3)->text().trimmed();
            str+="|";
            if (ui->tableScript->item(i,4)!=NULL) str+=ui->tableScript->item(i,4)->text().trimmed();
            str+="\n";
        }
    }
    //QMessageBox::information(this,"提示信息",str);
    QString curPath=QCoreApplication::applicationDirPath();//获取系统当前目录;//获取系统当前目录
    QString iniFile=QString("%1/chkScript.txt").arg(curPath);
    if (JCode::SaveText(iniFile,str)==false){
        QMessageBox::warning(this,"警告","文件保存失败！");
    }else{
        QMessageBox::information(this,"提示信息","文件保存成功！");
    }
}

void fromchknet::on_cmdScriptInsLine_clicked()
{
    int curRow = ui->tableScript->currentRow();
    if(curRow != ui->tableScript->rowCount()){
    //当不是最后一行时
        if(curRow == ui->tableScript->rowCount() - 1){
        //若为倒数第二行
            ui->tableScript->insertRow(curRow + 1);
            //在其下添加一行，即在最后一行上添加一行，但最后一行进行操作时是在末尾添加，因此先在末尾添加一行，使原来的最后一行变为倒数第二行，在该行基础上添加一行即在原来倒数第二行下；然后再删去末尾多添加的一行
            ui->tableScript->insertRow(curRow + 1);
            ui->tableScript->removeRow(curRow + 3);

        }
        else{
        //若为其他行
            ui->tableScript->insertRow(curRow + 1);

        }
    }
    else{
    //当为最后一行时
        ui->tableScript->insertRow(curRow);

    }

}


void fromchknet::on_cmdScriptDelLine_clicked()
{
    int curRow = ui->tableScript->currentRow();
    if (curRow != -1) ui->tableScript->removeRow(curRow);
}

void fromchknet::on_cmdScripCopy_clicked()
{
    JCode::FZTableWidget(ui->tableWidget);
}

void fromchknet::on_cmdScripPrint_clicked()
{
    QString curPath= QCoreApplication::applicationDirPath();//获取系统当前目录
    QString PdfFile=QString("%1/tmpPDF.pdf").arg(curPath);
    QDateTime time = QDateTime::currentDateTime();
    QString str = time.toString("yyyy年MM月dd日检测结果");
    JCode::TableWidgetTOPDF(ui->tableWidget,PdfFile,str,"","","横向");
}

void fromchknet::on_cmdClearTree_clicked()
{
    ui->treeWidgetIpScan->clear();
    ui->treeWidgetIpScan->setHeaderLabels(QStringList() << "IP" << "计算机名" << "MAC" );
    ui->treeWidgetIpScan->header()->setSectionResizeMode(QHeaderView::ResizeToContents); //QTreeWidget设置根据内容自适应宽度

}
void fromchknet::setNetworkGIF(const QString &GifUrl)
{
   QUrl url(GifUrl);
   QNetworkAccessManager manager;
   QEventLoop loop;

   // qDebug() << "图片下载地址： " << GifUrl;
   QNetworkReply *reply = manager.get(QNetworkRequest(url));
   //请求结束并下载完成后，退出子事件循环
   QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
   //开启子事件循环
   loop.exec();
   QByteArray gifmsg = reply->readAll();
   QString curPath= QCoreApplication::applicationDirPath();//获取当前目录
   QString gifname = QString("%1/logo.gif").arg(curPath);
       QFile file(gifname);
       if(!file.open(QFile::WriteOnly))
       {
           //qDebug() << "open file fail";
           exit(1);
       }
       file.write(gifmsg);
       file.close();
   QMovie *movie = new QMovie( gifname); //path是gif图片的绝对路径
   movie->setScaledSize(ui->labGif->size());
   ui->labGif->setMovie(movie);
   movie->start();
}

void fromchknet::on_cmdScriptStart_clicked()
{
    ScriptStart();
}

void fromchknet::AutoKeep()
{
    if (ui->cmbKeep->currentIndex()>0){
        if (ui->chkSpack->isChecked()==true){
            QString tmpstr="";
            tmpstr=ui->cmbKeep->currentText();
            JCode::SpeakText(tmpstr);
        }
        ScriptStart();
    }
}

void fromchknet::on_cmbKeep_currentIndexChanged(int index)
{
    long EndTinme=0;
    js=-1;
    if (index==1 ) EndTinme=1*1000*60;
    if (index==2 ) EndTinme=1*1000*60*10;
    if (index==3 ) EndTinme=1*1000*60*30;
    if (index==4 ) EndTinme=1*1000*60*60;
    if (index==5 ) EndTinme=1*1000*60*60*2;
    if (index==6 ) EndTinme=1*1000*60*60*3;
    if (index==7 ) EndTinme=1*1000*60*60*5;
    if (index==7 ) EndTinme=1*1000*60*60*6;
    if (index==8 ) EndTinme=1*1000*60*60*8;
    if (index==10 ) EndTinme=1*1000*60*60*12;
    if (index==11) EndTinme=1*1000*60*60*24;

    if (EndTinme!=0){
        js=EndTinme/1000;
        ui->lcdNumber->display(QString("%1").arg(js));
        m_timer->start(EndTinme); //固定时间调用一次
    }else{
        js=-1;
        ui->lcdNumber->display(QString("%1").arg(js));
    }
}
void fromchknet::DJS()
{

    if (js>0){
        js--;
    }else{
        if (js==0){
            int i=ui->cmbKeep->currentIndex();
            js=0;
            if (i==1 ) js=60;
            if (i==2 ) js=60*10;
            if (i==3 ) js=60*30;
            if (i==4 ) js=60*60;
            if (i==5 ) js=60*60*2;
            if (i==6 ) js=60*60*3;
            if (i==7 ) js=60*60*5;
            if (i==7 ) js=60*60*6;
            if (i==8 ) js=60*60*8;
            if (i==10) js=60*60*12;
            if (i==11) js=60*60*24;
        }

    }
    ui->lcdNumber->display(QString("%1").arg(js));
}
void fromchknet::RefPIcture(){
    //先删除所有子控件
    QList<QGroupBox*> Groups = ui->widgetPic->findChildren<QGroupBox*>();
    foreach (QGroupBox* Group, Groups) {
        //qDebug()<<Group->title();
        delete Group;
    }
    //列出所有存放部门加入QHBoxLayout
    //QHBoxLayout *Hlayout = new QHBoxLayout;
    QString Place="";
    QString AllPlace="";
    //先遍历 去重复
    for(int i=0;i<ui->tableScript->rowCount();i++){
        if(ui->tableScript->item(i,4)!=NULL ){
            if (Place!=ui->tableScript->item(i,4)->text().trimmed()){
                Place=ui->tableScript->item(i,4)->text().trimmed();
                QString tmpstr=QString("%1\n").arg(Place);
                if (!AllPlace.contains(tmpstr,Qt::CaseSensitive)){ //去重复
                    AllPlace+=tmpstr;
                }
            }
        }
    }
    QStringList strList = AllPlace.split("\n");
    //qDebug()<<AllPlace;
    //分割 建立GROUP
    for(int i = 0; i < strList.size()-1; i++) {
        QGroupBox *PlaceGroup =new QGroupBox(strList[i]);
        ui->Hlayout->addWidget(PlaceGroup);
    }
    int Hnum=ui->Hlayout->count();
    for (int i=0;i<Hnum;i++) {
        QLayoutItem *pw = ui->Hlayout->itemAt(i);
        QGroupBox *PlaceGroup = qobject_cast<QGroupBox *>(pw->widget());
        QString GroupStr=PlaceGroup->title();
        QVBoxLayout *layout = new QVBoxLayout;
        for(int j=0;j<ui->tableScript->rowCount();j++){
            if(ui->tableScript->item(j,4)!=NULL ){
                Place=ui->tableScript->item(j,4)->text().trimmed();
                if(Place==GroupStr){
                    QString tmpP="";
                    if(ui->tableScript->item(j,0)!=NULL ) tmpP+=ReplaceIimg(ui->tableScript->item(j,0)->text().trimmed());
                    tmpP+="<span>";
                    tmpP+=ui->tableScript->item(j,0)->text().trimmed();
                    tmpP+="</span>";
                    QLabel *P =new QLabel(tmpP);
                    P->setToolTip(QString("%1").arg(j));
                    layout->addWidget(P);
                }

            }
        }
        PlaceGroup->setLayout(layout);
    }

}
/*

void fromchknet::on_cmdScripShow_clicked()
{
    //列出所有存放部门加入QHBoxLayout
    //QHBoxLayout *Hlayout = new QHBoxLayout;
    QString Place="";
    QString AllPlace="";
    //先遍历 去重复
    for(int i=0;i<ui->tableScript->rowCount();i++){
        if(ui->tableScript->item(i,4)!=NULL ){
            if (Place!=ui->tableScript->item(i,4)->text().trimmed()){
                Place=ui->tableScript->item(i,4)->text().trimmed();
                QString tmpstr=QString("%1\n").arg(Place);
                if (!AllPlace.contains(tmpstr,Qt::CaseSensitive)){ //去重复
                    AllPlace+=tmpstr;
                }
            }
        }
    }
    QStringList strList = AllPlace.split("\n");
    //qDebug()<<AllPlace;
    //分割 建立GROUP
    for(int i = 0; i < strList.size()-1; i++) {
        QGroupBox *PlaceGroup =new QGroupBox(strList[i]);
        ui->Hlayout->addWidget(PlaceGroup);
    }
    int Hnum=ui->Hlayout->count();
    for (int i=0;i<Hnum;i++) {
        QLayoutItem *pw = ui->Hlayout->itemAt(i);
        QGroupBox *PlaceGroup = qobject_cast<QGroupBox *>(pw->widget());
        QString GroupStr=PlaceGroup->title();
        QVBoxLayout *layout = new QVBoxLayout;
        for(int j=0;j<ui->tableScript->rowCount();j++){
            if(ui->tableScript->item(j,4)!=NULL ){
                Place=ui->tableScript->item(j,4)->text().trimmed();
                if(Place==GroupStr){
                    QString tmpP="<img src=\":/tree/OK.ICO\"/><span>";
                    tmpP+=ui->tableScript->item(j,0)->text().trimmed();
                    tmpP+="</span>";
                    QLabel *P =new QLabel(tmpP);

                    layout->addWidget(P);
                }

            }
        }
        PlaceGroup->setLayout(layout);
    }


     *
     * int Hnum=Hlayout->count();
    for (int i=0;i<Hnum;i++) {
        QGroupBox *PlaceGroup = new QGroupBox(Hlayout->itemAt(i)->widget());
        qDebug()<<PlaceGroup;

    }
    QVBoxLayout *layout = new QVBoxLayout;
    for(int i;i<8;i++){
        QLabel *p =new QLabel("test1");
        layout->addWidget(p);
    }
    ui->groupBox->setLayout(layout);


                QGroupBox *PlaceGroup =new QGroupBox(Place);
                Hlayout->addWidget(PlaceGroup);
                Place=ui->tableScript->item(i,4)->text().trimmed();

void fromchknet::on_cmdScripDEL_clicked()
{
    QList<QGroupBox*> Groups = ui->widgetPic->findChildren<QGroupBox*>();
    //qDebug()<<Groups;
    foreach (QGroupBox* Group, Groups) {
        qDebug()<<Group->title();
        delete Group;
    }
    //删除父控件后子控件自动删除
    QList<QLabel*> GQLabels = ui->widgetPic->findChildren<QLabel*>();
    //qDebug()<<Groups;
    foreach (QLabel* GQLabel, GQLabels) {
        qDebug()<<GQLabel->text();
        delete GQLabel;
    }

}



}

*/

void fromchknet::on_cmdScripShow_clicked()
{
    if (ui->tableWidget->isVisible()==false){
        ui->tableWidget->setVisible(true);
        ui->widgetPic->setVisible(false);
    }else{
        ui->tableWidget->setVisible(false);
        ui->widgetPic->setVisible(true);
    }

}
